package com.dragon.gallery_paging

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

class PagerPhotoListAdapter : ListAdapter<PhotoItem,PagerPhotoViewHolder>(DiffCallback){

    object DiffCallback:DiffUtil.ItemCallback<PhotoItem>(){
        override fun areItemsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem.id == newItem.id
        }

        @SuppressLint("DiffUtilEquals")
        override fun areContentsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem === newItem
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagerPhotoViewHolder {
        LayoutInflater.from(parent.context).inflate(R.layout.pager_photo_view,parent,false).apply {
            return PagerPhotoViewHolder(this)
        }
    }
    override fun onBindViewHolder(holder: PagerPhotoViewHolder, position: Int) {
        holder.pagerPhoto?.let {
            Glide.with(holder.itemView)
                .load(getItem(position).preViewUrl)
                .placeholder(R.drawable.photo_placeholfer)
                .into(it)
        }
    }

}

class PagerPhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
     var pagerPhoto:ImageView? = null

    init {
        pagerPhoto = itemView.findViewById(R.id.pagerPhoto)
    }

}